package com.cloud.flowable.controller;

import com.cloud.common.domain.Result;
import com.cloud.controller.BaseController;
import com.cloud.enums.BusinessType;
import com.cloud.flowable.entity.WfCategory;
import com.cloud.flowable.entity.bo.ModelPage;
import com.cloud.flowable.entity.vo.WfModelVo;
import com.cloud.flowable.service.IWfCategoryService;
import com.cloud.flowable.service.IWfModelService;
import com.cloud.logs.Logs;
import com.cloud.security.aop.RequestPermissions;
import com.cloud.utils.ExcelPoiUtils;
import com.cloud.utils.ServletUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Tag(name = "流程模型管理")
@RestController
@RequestMapping("/flowable/Model")
public class WfModelController extends BaseController {

    @Autowired
    private IWfModelService modelService;

    @Autowired
    private IWfCategoryService categoryService;

    @Operation(summary = "流程模型分页列表")
    @RequestPermissions("flowable:model:list")
    @GetMapping("/list")
    public Result list(ModelPage modelPage) {
        return Result.success("查询成功", modelService.getModelPage(modelPage));
    }

    @Operation(summary = "新增流程模型")
    @RequestPermissions("flowable:model:add")
    @Logs(title = "新增流程模型", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public Result saveModel(@RequestBody ModelPage modelPage) {
        modelService.insertModel(modelPage);
        return Result.success("操作成功");
    }

    @Operation(summary = "查询流程模型分页列表")
    @GetMapping("/historyList")
    public Result historyList(ModelPage modelPage) {
        return Result.success("查询成功", modelService.historyList(modelPage));
    }

    @Operation(summary = "获取流程模型详细信息")
    @RequestPermissions("flowable:model:query")
    @GetMapping(value = "/getInfo")
    public Result getInfo(String modelId) {
        WfModelVo model = modelService.getModel(modelId);
        return Result.success("", model);
    }

    @Operation(summary = "获取流程表单详细信息")
    @RequestPermissions("flowable:model:query")
    @GetMapping(value = "/bpmnXml")
    public Result getBpmnXml(String modelId) {
        return Result.success("操作成功", modelService.queryBpmnXmlById(modelId));
    }

    @Operation(summary = "修改流程模型")
    @RequestPermissions("flowable:model:edit")
    @Logs(title = "流程模型", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public Result edit(@RequestBody ModelPage modelPage) {
        modelService.updateModel(modelPage);
        return Result.success("保存成功");
    }

    @Operation(summary = "保存流程模型")
    @RequestPermissions("flowable:model:save")
    @Logs(title = "保存流程模型", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public Result save(@RequestBody ModelPage modelPage) {
        modelService.saveModel(modelPage);
        return Result.success("保存成功");
    }

    @Operation(summary = "设为最新流程模型")
    @RequestPermissions("flowable:model:save")
    @Logs(title = "设为最新流程模型", businessType = BusinessType.INSERT)
    @PostMapping("/latest")
    public Result latest(@RequestParam String modelId) {
        modelService.latestModel(modelId);
        return Result.success("保存成功");
    }

    @Operation(summary = "删除流程模型")
    @RequestPermissions("flowable:model:remove")
    @Logs(title = "删除流程模型", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public Result remove(String[] modelIds) {
        modelService.deleteByIds(Arrays.asList(modelIds));
        return Result.success("删除成功");
    }

    @Operation(summary = "部署流程模型")
    @RequestPermissions("flowable:model:deploy")
    @Logs(title = "部署流程模型", businessType = BusinessType.INSERT)
    @PostMapping("/deploy")
    public Result deployModel(@RequestParam String modelId) {
        return Result.success("", modelService.deployModel(modelId));
    }

    @Operation(summary = "导出流程模型数据")
    @RequestPermissions("flowable:model:export")
    @Logs(title = "导出流程模型数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ModelPage modelPage) {
        List<WfModelVo> list = modelService.getModelList(modelPage);
        List<WfCategory> categoryVos = categoryService.getCategoryList(new WfCategory());
        Map<String, String> categoryMap = categoryVos.stream()
                .collect(Collectors.toMap(WfCategory::getCode, WfCategory::getCategoryName));
        for (WfModelVo modelVo : list) {
            modelVo.setCategoryName(categoryMap.get(modelVo.getCategory()));
        }
        String titleName = "流程模型数据";
        ExcelPoiUtils.exportExcel(list, titleName, "流程模型数据",
                WfModelVo.class, titleName + ".xls", ServletUtils.getResponse());
    }

}
